#pragma once
#include <sys/types.h>
#include <sys/socket.h>
#include <iostream>
#include <arpa/inet.h>
#include <cstring>
#include <unistd.h>
#include <fcntl.h>



#define BACKLOG 5
#define SOCKFD -1

void SetNonBlock(int fd)
{
    int sig = fcntl(fd, F_GETFL);
    if(sig < 0)
    {
        std::cerr << "fcntl error" << std::endl;
        return;
    }
    fcntl(fd, F_SETFL, sig | O_NONBLOCK);
}

class Socket
{
public:
    virtual void CreateSocket() = 0;
    virtual void BindSocket(int) = 0;
    virtual void ListenSocket() = 0;
    virtual int AcceptSocket(std::string*, int*) = 0;
    virtual int GetSocket() = 0;
    virtual bool Recv(char*, int) = 0;
    virtual void Send(std::string&) = 0;
    virtual void Close() = 0;
public:
    void BulidListenSocket(int port)
    {
        CreateSocket();
        BindSocket(port);
        ListenSocket();
    }
};

class TcpSocket : public Socket
{
public:
    TcpSocket(int sockfd = SOCKFD) : _sockfd(sockfd)
    {}
    ~TcpSocket()
    {
        // close(_sockfd);
    }

    void CreateSocket()
    {
        _sockfd = socket(AF_INET, SOCK_STREAM, 0);
        if(_sockfd < 0)
        {
            std::cerr << "创建套接字错误" << std::endl;
            exit(1);
        }
        // 端口复用
        int reuse = 1;
        setsockopt(_sockfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse));
        // 设置fd为非阻塞
        SetNonBlock(_sockfd);
    }
    void BindSocket(int port)
    {
        struct sockaddr_in local;
        memset(&local, 0, sizeof(local));
        local.sin_family = AF_INET;
        local.sin_port = htons(port);
        local.sin_addr.s_addr = INADDR_ANY;

        socklen_t len = sizeof(local);
        if(bind(_sockfd, (sockaddr*)&local, len) < 0)
        {
            std::cerr << "绑定失败" << std::endl;
            exit(2);
        }
    }  
    void ListenSocket()
    {
        if(listen(_sockfd, BACKLOG) < 0)
        {
            std::cerr << "监听失败" << std::endl;
            exit(3);
        }
    }
    int AcceptSocket(std::string *peerip, int *peerport)
    {
        struct sockaddr_in peer;
        memset(&peer, 0, sizeof(peer));
        socklen_t len = sizeof(peer);
        int newsockfd = accept(_sockfd, (struct sockaddr*)&peer, &len);
        if(newsockfd < 0)
        {
            std::cerr << "获取套接字error" << std::endl;
            exit(4);
        }
        *peerip = inet_ntoa(peer.sin_addr);
        *peerport = ntohs(peer.sin_port);
        return newsockfd;
    }
    void ConnectServer(char *peerip, uint16_t peerport)
    {
        struct sockaddr_in peer;
        memset(&peer, 0, sizeof(peer));
        peer.sin_family = AF_INET;
        peer.sin_port = htons(peerport);
        peer.sin_addr.s_addr = inet_addr(peerip);
        socklen_t len = sizeof(peer);
        if(connect(_sockfd, (struct sockaddr*)&peer, len) < 0)
        {
            std::cout << "connect error" << std::endl;
            exit(5);
        }
    }
    int GetSocket()
    {
        return _sockfd;
    }
    bool Recv(char *buffer, int size)
    {
        int n = recv(_sockfd, buffer, size - 1, 0);
        if(n > 0) 
        {
            buffer[n] = '\0';
            return true;
        }
        return false;
    }
    void Send(std::string &buffer)
    {
        send(_sockfd, buffer.c_str(), buffer.size(), 0);
    }
    void Close()
    {
        close(_sockfd);
    }

private:
    int _sockfd;
};
